package cn.tedu.ivos.vehicle.service.imp;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImp implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {

        List<VehicleVO> vehicles = vehicleMapper.selectVehicle(vehicleQuery);
        log.debug("参数: {}",vehicles);

        return vehicles;

    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("保存车辆业务: {}",vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if (vehicle.getId()==null){
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.saveVehicle(vehicle);

        }else {
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("删除业务: {}");
        vehicleMapper.deleteVehicle(id);
    }
    @Override
    public void updateNull(Long id) {
        log.debug("解绑业务: {}",id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceBindStatus("0");
        vehicleMapper.updateNull(vehicle);
    }
    @Override
    public void bindVehicle(VehicleQuery vehicleQuery) {
        log.debug("绑定业务: {}",vehicleQuery);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleQuery.getId());
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(vehicleQuery.getGeofenceId());
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
